package com.rookie.im.learn.io.mq.consumer.headers;

import com.rookie.im.learn.io.mq.config.RabbitMQConfig;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.Map;

@Component
public class ConsumerServiceHeaders {
    @RabbitListener(queuesToDeclare = @Queue(RabbitMQConfig.RABBITMQ_DEMO_HEADERS_EXCHANGE_QUEUE_A))
    public void processA(Message message) {
        MessageProperties messageProperties = message.getMessageProperties();
        System.out.println("收到队列A消息: " + getString(message));
    }
    @RabbitListener(queuesToDeclare = @Queue(RabbitMQConfig.RABBITMQ_DEMO_HEADERS_EXCHANGE_QUEUE_B))
    public void processB(Message message) {
        MessageProperties messageProperties = message.getMessageProperties();
        System.out.println("收到队列B消息: " + getString(message));
    }
    private static String getString(Message message)  {
        MessageProperties messageProperties = message.getMessageProperties();
        try {
            return new String(message.getBody(), messageProperties.getContentType());
        } catch (Exception e) {
            e.printStackTrace();
            return "消息解析错误: " + e;
        }
    }
}
